package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.constant.RedisConstant;
import com.itheima.dao.SetmealDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.Setmeal;
import com.ithema.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.JedisPool;

import java.util.HashMap;
import java.util.List;

@Service(interfaceClass = SetmealService.class)//指定类,不然会自动随机指定类
@Transactional //事务注解
public class SetmealServiceImpl implements SetmealService {
    /**
     * 新增套餐信息
     * @param setmeal
     * @param checkgroupIds
     */
    @Autowired
    private JedisPool jedisPool;

    @Autowired
    private SetmealDao setmealDao;
    public void add(Setmeal setmeal, Integer[] checkgroupIds) {

            //编写dao接口,需要2个add(Setmeal setmeal), 一个中间关系表setSetmealAndCheckGroup(Map,map)
            //新增套餐信息, 要关联检查组.
       //调用Dao层方法
        setmealDao.add(setmeal);
        //返回一个主键ID,来关联中间表
        Integer setmealId= setmeal.getId();
        this.setSetmealAndCheckGroup(setmealId,checkgroupIds);
        //新增套餐,完成后把图片保存到redis集合中
        String fileName = setmeal.getImg();
        //上传成或后,上传一张图片到redis小集合中.
        jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_DB_RESOURCES,fileName);

    }

    @Override
    public PageResult pageQuery(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage(); //当前页码
        Integer pageSize = queryPageBean.getPageSize(); //当前条数
        String queryString = queryPageBean.getQueryString(); //查询条件
        //myabatis框架里的方法
        PageHelper.startPage(currentPage, pageSize);
         Page<Setmeal>page=setmealDao.selectByCondition(queryString);
        long total = page.getTotal();
        List<Setmeal> rows = page.getResult();
        return new PageResult(total,rows);
    }

    @Override
    public Setmeal findById(Integer id) {


        return setmealDao.findById(id);
    }

    @Override
    public void edit(CheckGroup checkGroup, Integer[] checkitemIds) {

    }

    @Override
    public List<Setmeal> findAll() {

        return  setmealDao.findAll();
    }

    @Override
    public Setmeal findByIds(int id) {
       return setmealDao.findByIds(id);
    }


    //设置套餐和检查组多对多关系,操作t_setmeal_checkgroup中间表
    public void setSetmealAndCheckGroup(Integer setmealId, Integer[] checkitmIds){
           //判断检查组id是否为空,或者长度大于0
            if (checkitmIds !=null && checkitmIds.length>0){
                  //遍历checkitmIds
                for (Integer checkitmId : checkitmIds) {
                    HashMap<String,Integer> map = new HashMap<>();
                map.put("setmealId",setmealId);
                map.put("checkgroupId",checkitmId);
               setmealDao.setSetmealAndCheckGroup(map);

                }

            }

    }


}



